﻿//	Copyright (c) 2013 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ordinaire.Db;

namespace Ordinaire
{
    /// <summary>
    /// Represents a base model of data manager which provides common business logic.
    /// </summary>
    /// <typeparam name="TDataManager">data manager type derived from Ordinaire.DataManager</typeparam>
    /// <typeparam name="TOContext">data context type derived from Ordinaire.Db.OContext</typeparam>
    /// <remarks>This is provided to give the convenience for all derived classes the ability to share data context.</remarks>
    public abstract class BusinessManager<TDataManager, TOContext>
        where TDataManager : DataManager<TOContext>
        where TOContext    : OContext
    {
        /// <summary>
        /// Storage to hold Ordinaire.DataManager instance.
        /// </summary>
        protected TDataManager dataManager;

        /// <summary>
        /// Gets or sets the data context used by derived class of Ordinaire.BusinessManager.
        /// </summary>
        public virtual TOContext Context
        {
            get { return this.dataManager.Context; }
            set { this.dataManager.Context = value; }
        }

        /// <summary>
        /// Shares data context to the specified Ordinaire.BusinessManager.
        /// </summary>
        /// <param name="manager">the business manager to share with</param>
        /// <remarks>This is used to prevent from needing MSDTC to be activated</remarks>
        public virtual void ShareContext<TManager, TContext>(BusinessManager<TManager, TContext> manager)
            where TManager : DataManager<TContext>
            where TContext : OContext
        {
            manager.Context = (TContext) (object) Context;
        }
    }
}
